home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in Java / c08 / newcollections / Collection1.java < prev    next >
Encoding:
Java Source  |  2000-05-25  |  5.2 KB  |  142 lines

  1. //: Collection1.java
  2. //////////////////////////////////////////////////
  3. // Copyright (c) Bruce Eckel, 1998
  4. // Source code file from the book "Thinking in Java"
  5. // All rights reserved EXCEPT as allowed by the
  6. // following statements: You can freely use this file
  7. // for your own work (personal or commercial),
  8. // including modifications and distribution in
  9. // executable form only. Permission is granted to use
  10. // this file in classroom situations, including its
  11. // use in presentation materials, as long as the book
  12. // "Thinking in Java" is cited as the source. 
  13. // Except in classroom situations, you cannot copy
  14. // and distribute this code; instead, the sole
  15. // distribution point is http://www.BruceEckel.com 
  16. // (and official mirror sites) where it is
  17. // freely available. You cannot remove this
  18. // copyright and notice. You cannot distribute
  19. // modified versions of the source code in this
  20. // package. You cannot use this file in printed
  21. // media without the express permission of the
  22. // author. Bruce Eckel makes no representation about
  23. // the suitability of this software for any purpose.
  24. // It is provided "as is" without express or implied
  25. // warranty of any kind, including any implied
  26. // warranty of merchantability, fitness for a
  27. // particular purpose or non-infringement. The entire
  28. // risk as to the quality and performance of the
  29. // software is with you. Bruce Eckel and the
  30. // publisher shall not be liable for any damages
  31. // suffered by you or any third party as a result of
  32. // using or distributing software. In no event will
  33. // Bruce Eckel or the publisher be liable for any
  34. // lost revenue, profit, or data, or for direct,
  35. // indirect, special, consequential, incidental, or
  36. // punitive damages, however caused and regardless of
  37. // the theory of liability, arising out of the use of
  38. // or inability to use software, even if Bruce Eckel
  39. // and the publisher have been advised of the
  40. // possibility of such damages. Should the software
  41. // prove defective, you assume the cost of all
  42. // necessary servicing, repair, or correction. If you
  43. // think you've found an error, please email all
  44. // modified files with clearly commented changes to:
  45. // Bruce@EckelObjects.com. (Please use the same
  46. // address for non-code errors found in the book.)
  47. /////////////////////////////////////////////////
  48.  
  49. // Things you can do with all Collections
  50. package c08.newcollections;
  51. import java.util.*;
  52.  
  53. public class Collection1 {
  54.   // Fill with 'size' elements, start
  55.   // counting at 'start':
  56.   public static Collection 
  57.   fill(Collection c, int start, int size) {
  58.     for(int i = start; i < start + size; i++)
  59.       c.add(Integer.toString(i));
  60.     return c;
  61.   }
  62.   // Default to a "start" of 0:
  63.   public static Collection 
  64.   fill(Collection c, int size) {
  65.     return fill(c, 0, size);
  66.   }
  67.   // Default to 10 elements:
  68.   public static Collection fill(Collection c) {
  69.     return fill(c, 0, 10);
  70.   }
  71.   // Create & upcast to Collection:
  72.   public static Collection newCollection() {
  73.     return fill(new ArrayList());
  74.     // ArrayList is used for simplicity, but it's
  75.     // only seen as a generic Collection 
  76.     // everywhere else in the program.
  77.   }
  78.   // Fill a Collection with a range of values:
  79.   public static Collection 
  80.   newCollection(int start, int size) {
  81.     return fill(new ArrayList(), start, size);
  82.   }
  83.   // Moving through a List with an iterator:
  84.   public static void print(Collection c) {
  85.     for(Iterator x = c.iterator(); x.hasNext();)
  86.       System.out.print(x.next() + " ");
  87.     System.out.println();
  88.   }    
  89.   public static void main(String[] args) {
  90.     Collection c = newCollection();
  91.     c.add("ten");
  92.     c.add("eleven");
  93.     print(c);
  94.     // Make an array from the List:
  95.     Object[] array = c.toArray(); 
  96.     // Make a String array from the List:
  97.     String[] str = 
  98.       (String[])c.toArray(new String[1]);
  99.     // Find max and min elements; this means
  100.     // different things depending on the way
  101.     // the Comparable interface is implemented:
  102.     System.out.println("Collections.max(c) = " +
  103.       Collections.max(c));
  104.     System.out.println("Collections.min(c) = " +
  105.       Collections.min(c));
  106.     // Add a Collection to another Collection
  107.     c.addAll(newCollection());
  108.     print(c);
  109.     c.remove("3"); // Removes the first one
  110.     print(c);
  111.     c.remove("3"); // Removes the second one
  112.     print(c);
  113.     // Remove all components that are in the
  114.     // argument collection:
  115.     c.removeAll(newCollection());
  116.     print(c);
  117.     c.addAll(newCollection());
  118.     print(c);
  119.     // Is an element in this Collection?
  120.     System.out.println(
  121.       "c.contains(\"4\") = " + c.contains("4"));
  122.     // Is a Collection in this Collection?
  123.     System.out.println(
  124.       "c.containsAll(newCollection()) = " + 
  125.       c.containsAll(newCollection()));
  126.     Collection c2 = newCollection(5, 3);
  127.     // Keep all the elements that are in both
  128.     // c and c2 (an intersection of sets):
  129.     c.retainAll(c2);
  130.     print(c);
  131.     // Throw away all the elements in c that
  132.     // also appear in c2:
  133.     c.removeAll(c2);
  134.     System.out.println("c.isEmpty() = " +
  135.       c.isEmpty());
  136.     c = newCollection();
  137.     print(c);
  138.     c.clear(); // Remove all elements
  139.     System.out.println("after c.clear():");
  140.     print(c);
  141.   }
  142. } ///:~